﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace VisualTrajectory
{
    public class TrajectoryCollection
    {
        public List<Trajectory> m_TrajList = new List<Trajectory>();

        public TrajectoryCollection()
        {
        }

        public void Add(Trajectory traj)
        {
            m_TrajList.Add(traj);
        }

        public void Delete(int index)
        {
            m_TrajList.RemoveAt(index);
        }

        public int Count()
        {
            return m_TrajList.Count;
        }

        public Trajectory GetTrajectoryByIndex(int nIndex)
        {
            return m_TrajList[nIndex];
        }

        public int GetTrajectoryIndexByName(string name)
        {
            for (int i = 0; i < m_TrajList.Count; i++)
            {
                if (m_TrajList[i].GetName().CompareTo(name) == 0)
                    return i;
            }
            return -1;
        }

        public Trajectory GetBack()
        {
            return m_TrajList[m_TrajList.Count - 1];
        }

        public void SetTrajectory(int nIndex, Trajectory traj)
        {
            m_TrajList[nIndex] = traj;
        }

        public int LoadFromTxtFile(string strFilename)
        {
            try
            {
                char[] separators = {',',' ','\t'};
                // Open the file
                FileStream file = File.Open(strFilename, FileMode.Open);
                // Create the stream reader
                StreamReader streamReader = new StreamReader(file);
                // Rerad first line
                string strLine = streamReader.ReadLine();
                string strLastTrajName = "";
                PointF p = new PointF();
                int nLineCount = 0;
                // Read each line in the txt file
                while (strLine != null && strLine.Length > 0)
                {
                    string[] strElems = strLine.Split(separators);
                    if (strLastTrajName.Length == 0 || strLastTrajName.CompareTo(strElems[0]) != 0)
                    {
                        // Add new trajectory
                        Trajectory traj = new Trajectory();
                        traj.SetName(strElems[0]);
                        this.Add(traj);
                        strLastTrajName = strElems[0];
                    }
                    // Parse the data point and Add to this trajectory
                    p.X = (float)Double.Parse(strElems[1]);
                    p.Y = (float)Double.Parse(strElems[2]);
                    this.GetBack().AddPoint(p);

                    // Read next line
                    strLine = streamReader.ReadLine();
                    nLineCount++;
                }
                // Close file
                streamReader.Close();
                file.Close();

                return 0;
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
                return -1;
            }
        }

        public void WriteToStream(StreamWriter writer)
        {
            for (int i = 0; i < m_TrajList.Count; i++)
            {
                m_TrajList[i].WriteToStream(writer);
            }
        }
    }
}
